var batchlist = {};
batchlist.bizKey = ""; // 缓存页面对应的业务主键
batchlist.$planSkuBatchTable = null;
batchlist.$realSkuBatchTable = null;

$(function() {
    // 从URL上获取业务主键
    batchlist.bizKey = extractParam("busiKey");
    if (!!batchlist.bizKey) {
        batchlist.operation.refreshNoteList();
    }
});

batchlist.operation = {};

batchlist.operation.refreshNoteList = function() {
    batchlist.$planSkuBatchTable = $("#planSkuBatchTable").DataTable(getDataTableOption("plan")).on('draw.dt', function() {
        $("#planSkuBatchTable").tooltip({
            selector : ".tooltip-column-detail",
            container : "body",
            title : function() {
                if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                    return $(this).text();
                }
            }
        });
    });
    
    batchlist.$realSkuBatchTable = $("#realSkuBatchTable").DataTable(getDataTableOption("real")).on('draw.dt', function() {
        $("#realSkuBatchTable").tooltip({
            selector : ".tooltip-column-detail",
            container : "body",
            title : function() {
                if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                    return $(this).text();
                }
            }
        });
    });
}

// =============公共方法=============

function getDataTableOption(dataType){
    return {
        "processing" : true,
        "serverSide" : true,
        "dom" : '<"bottom"rtflp>',
        "searching" : false,
        "scrollY" : "140px",
        "scrollCollapse" : false,
        "scrollX": true,
        "destroy" : true,
        "paging" : false,
        "ordering" : false,
        "deferRender" : true,
        "ajax" : {
            "url" : "/batch/getBatchListBySourceId.action",
            "type" : "get",
            "data" : {
                "sourceId" : batchlist.bizKey,
                "kind":dataType
            }
        },
        "columns" : [{
            "className" : "truncate-text tooltip-column-detail",
            "data" : "storName"
        }, {
            "className" : "truncate-text tooltip-column-detail",
            "data" : "goodsCode"
        }, {
            "className" : "truncate-text tooltip-column-detail",
            "data" : "goodsName"
        }, {
            "className" : "truncate-text tooltip-column-detail",
            "data" : "skuCode"
        }, {
            "className" : "truncate-text tooltip-column-detail",
            "data" : "skuName"
        }, {
            "data" : "inventoryType",
            "render" : function(data){
                if(data === "zp"){
                    return "正品";
                }else if(data === "cc"){
                    return "次品";
                }
            }
        }, {
            "data" : "qty"
        }, {
            "className" : "truncate-text tooltip-column-detail",
            "data" : "batchCode"
        }, {
            "data" : "productDate"
        }, {
            "data" : "expireDate"
        } ]
    }
};

// 解析某个URL中的参数的取值
function GetArgsFromHref(sHref, sArgName) {
    var args = sHref.split("?");
    var retval = "";

    if (args[0] == sHref) /* 参数为空 */
    {
        return retval; /* 无需做任何处理 */
    }
    var str = args[1];
    args = str.split("&");
    for (var i = 0; i < args.length; i++) {
        str = args[i];
        var arg = str.split("=");
        if (arg.length <= 1)
            continue;
        if (arg[0] == sArgName)
            retval = arg[1];
    }
    return retval;
}

// 从当前网页的URL中获取参数取值
function extractParam(paramName) {
    var returnString = "";
    var str = window.location.search;
    if (!!str) {
        returnString = GetArgsFromHref(str, paramName);
    }
    return returnString;
}